package cn.ilikes.tools.hbase.jpa.metadatas.processor.validators;

import java.lang.reflect.Field;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.annotations.Index;
import cn.ilikes.tools.hbase.jpa.annotations.IndexColumn;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;

import cn.ilikes.tools.logs.Loggerfactory;

public class EntityIndexValidator implements EntityValidator {

	private Logger logger = Loggerfactory.instance(EntityIndexValidator.class);

	@Override
	public boolean entityValidator(Class<?> clazz, IEntityMetaData entityMetaData) throws EntityValidatorException {
		if (Loggerfactory.isdebug(logger))
			logger.debug("start validator entity   proper set   @Index   and  @IndexColumn ");
		if (clazz.isAnnotationPresent(Index.class)) {
			if (!clazz.isAnnotationPresent(Entity.class))
				throw new EntityValidatorException("this  entity : " + clazz.getSimpleName() + " find @Index ,but not find @Entity !  pealse set  @Index to class ! ");
			else {
				return true;
			}

		} else {
			//设置@IndexColumn but 没有设置  @Index
			for (Field f : clazz.getDeclaredFields()) {
				if (f.isAnnotationPresent(IndexColumn.class))
					throw new EntityValidatorException("this  entity : " + clazz.getSimpleName()
							+ " find @IndexColumn ,but not find @Index pealse set  @Index to class ! ");

			}
			//没 设 @Index 
			return true;
			//throw new EntityValidatorException("this  entity : " + clazz.getCanonicalName() + " not find @Index ");
		}

	}

}
